﻿using MN.Framework.Common;
using MN.Framework.Common.Service;
using MN.Framework.UI.Resource;
using MN.Framework.UI.Win.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;

namespace MN.Framework.UI.Win.Forms
{
    public abstract class MnMainForm : MnBaseForm
    {
        #region Property
        protected Telerik.WinControls.UI.RadPageView MainPageView { get; set; }
        private Telerik.WinControls.UI.RadMenu menuTop;
        public RadMenuItem SecurityMenu;
        public RadMenuItem SettingMenu { get; set; }
        public RadMenuItem HelpMenu { get; set; }
        public RadMenuItem FacilitiesMenu { get; set; }
        public abstract BaseContext DBContext { get; }
        List<Type> ExistEntity { get; set; }
        public enum SubMenuType
        {
            File,
            Security,
            Setting,
            Help
        }
        System.Drawing.Size formSizeDefault;
        public virtual System.Drawing.Size FormSizeDefault
        {
            get
            {
                if (formSizeDefault.IsEmpty)
                    formSizeDefault = new System.Drawing.Size(900, 550);
                return formSizeDefault;
            }
            set
            {
                formSizeDefault = value;
            }
        }
        #endregion

        public MnMainForm()
        {
            InitializeComponent();
        }

        #region initMethod
        private void InitializeComponent()
        {
            this.ExistEntity = new List<Type>();
            this.Size = FormSizeDefault;
            InitTopMenu();
            InitMainPageView();
        }

        public virtual void InitMainPageView()
        {
            // 
            // radPageView1
            // 
            MainPageView = new RadPageView();
            ((System.ComponentModel.ISupportInitialize)(this.MainPageView)).BeginInit();
            MainPageView.Dock = System.Windows.Forms.DockStyle.Bottom;
            MainPageView.Location = new System.Drawing.Point(0, 49);
            MainPageView.Name = "MainPageView";
            MainPageView.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            MainPageView.Size = new System.Drawing.Size(this.Width - 10, (this.Height - 30));  //new System.Drawing.Size(986, 519);
            MainPageView.TabIndex = 2;
            MainPageView.Text = "MainPageView";
            this.Controls.Add(this.MainPageView);
            this.Controls.SetChildIndex(this.MainPageView, 0);
            ((System.ComponentModel.ISupportInitialize)(this.MainPageView)).EndInit();
        }

        public virtual void InitTopMenu()
        {
            this.menuTop = new Telerik.WinControls.UI.RadMenu();
            this.SecurityMenu = new Telerik.WinControls.UI.RadMenuItem();
            this.FacilitiesMenu = new Telerik.WinControls.UI.RadMenuItem();
            //((System.ComponentModel.ISupportInitialize)(this.radMenu1)).BeginInit();
            // 
            // --------------TopMenu--------------------
            //             
            this.menuTop.Location = new System.Drawing.Point(5, 10);
            this.menuTop.Name = "TopMenu";
            this.menuTop.Size = new System.Drawing.Size(this.Width, 20);
            this.menuTop.TabIndex = 0;
            this.menuTop.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            this.menuTop.Margin = new System.Windows.Forms.Padding(10);

            // File Menu
            #region FileMenu
            var FileMenu = new Telerik.WinControls.UI.RadMenuItem();
            FileMenu.AccessibleDescription = "FileMenu";
            FileMenu.AccessibleName = "FileMenu";
            FileMenu.Name = "FileMenu";
            FileMenu.Text = TopMenu.File;
            FileMenu.Visibility = Telerik.WinControls.ElementVisibility.Visible;
            //this.radMenuItemFilter.Image = ResourceImages.zoomin32;
            //File Items
            RadMenuItem itemNew = new RadMenuItem();
            itemNew.Text = TopMenu.New;
            itemNew.Name = "New";

            RadMenuItem itemOpen = new RadMenuItem();
            itemOpen.Text = TopMenu.Open;
            itemOpen.Name = "Open";

            RadMenuItem itemRestore = new RadMenuItem();
            itemRestore.Text = TopMenu.Restore;
            itemRestore.Name = "Restore";

            RadMenuItem itemBackUp = new RadMenuItem();
            itemBackUp.Text = TopMenu.BackUp;
            itemBackUp.Click += delegate(object sender, EventArgs e)
            {
                var fileName = "BackUpEntities" + DateTime.Now.ToString("yyyyMMdd") + ".Xml";
                MN.Framework.Common.Xml.SaveXmlFile(MN.Framework.Common.Xml.EntitesAsXml(DBContext, ExistEntity), fileName);
                System.Diagnostics.Process.Start("notepad.exe", fileName);
            };
            itemBackUp.Name = "BackUp";

            RadMenuItem itemExit = new RadMenuItem();
            itemExit.Text = TopMenu.Exit;
            itemExit.Click += delegate(object sender, EventArgs e)
            {
                Application.Exit();
            };
            itemExit.Name = "Exit";

            FileMenu.Items.Add(itemNew);
            FileMenu.Items.Add(itemOpen);
            FileMenu.Items.Add(itemRestore);
            FileMenu.Items.Add(itemBackUp);
            FileMenu.Items.Add(itemExit);

            menuTop.Items.Add(FileMenu);
            #endregion

            // Edit Menu     
            #region EditMenu
            var EditMenu = new Telerik.WinControls.UI.RadMenuItem();
            EditMenu.AccessibleDescription = "EditMenu";
            EditMenu.AccessibleName = "EditMenu";
            EditMenu.Name = "EditMenu";
            EditMenu.Text = TopMenu.Edit;
            EditMenu.Visibility = Telerik.WinControls.ElementVisibility.Visible;
            //this.radMenuItem3.Image = ResourceImages.refresh32;
            //----Menu Items----
            RadMenuItem itemCopy = new RadMenuItem();
            itemCopy.Text = TopMenu.Copy;
            itemNew.Name = "Copy";

            RadMenuItem itemPaste = new RadMenuItem();
            itemPaste.Text = TopMenu.Paste;
            itemPaste.Name = "Paste";

            RadMenuItem itemCut = new RadMenuItem();
            itemCut.Text = TopMenu.Cut;
            itemCut.Name = "Cut";

            RadMenuItem itemSelectAll = new RadMenuItem();
            itemSelectAll.Text = TopMenu.SelectAll;
            itemSelectAll.Name = "SelectAll";

            EditMenu.Items.Add(itemCopy);
            EditMenu.Items.Add(itemPaste);
            EditMenu.Items.Add(itemCut);
            EditMenu.Items.Add(itemSelectAll);
            menuTop.Items.Add(EditMenu);
            #endregion

            #region FacilitiesMenu
            FacilitiesMenu.Name = "FacilitiesMenu";
            FacilitiesMenu.Text = TopMenu.Facilities;
            //----Items-----
            var scedulerItem = new RadMenuItem("زمانبندی");
            scedulerItem.Click += delegate(object sender, EventArgs e)
            {
                (new SchedulerForm(DBContext)).ShowDialog();
            };
            FacilitiesMenu.Items.Add(scedulerItem);

            menuTop.Items.Add(FacilitiesMenu);
            #endregion

            #region SecurityMenu
            SecurityMenu.Name = "SecurityMenu";
            SecurityMenu.Text = TopMenu.Security;
            //----Items-----
            menuTop.Items.Add(SecurityMenu);
            #endregion

            #region ExecuteMenu
            var ExecuteMenu = new RadMenuItem(TopMenu.Execute);
            var dbItem = new RadMenuItem(Resource.TopMenu.DBCommand);
            dbItem.Click += delegate(object sender, EventArgs e)
            {
                (new Forms.CommandForm()).ShowDialog();
            };
            ExecuteMenu.Items.Add(dbItem);
            menuTop.Items.Add(ExecuteMenu);
            #endregion

            #region SettingMenu
            SettingMenu = new RadMenuItem(TopMenu.BaseSetting);
            menuTop.Items.Add(SettingMenu);
            #endregion
            #region ConfigurationMenu
            var ConfigurationMenu = new RadMenuItem(TopMenu.Configuration);
            menuTop.Items.Add(ConfigurationMenu);
            #endregion
            #region HelpMenu
            var HelpMenu = new RadMenuItem(TopMenu.Help);
            menuTop.Items.Add(HelpMenu);
            #endregion

            this.Controls.Add(this.menuTop);
        }

        #endregion
        public void AddSubMenuEntity<EntityType>(SubMenuType type, BaseContext dbContext, string MenuText = "") where EntityType : BaseEntity
        {
            var t = typeof(EntityType);
            if (string.IsNullOrEmpty(MenuText))
            {
                MenuText = ServiceFactory.Create<EntityType>().getTitle();
            }
            var subMenuItem = new RadMenuItem(MenuText);
            subMenuItem.Name = t.GetType().Name;
            subMenuItem.Click += delegate(object sender, EventArgs e)
            {
                RadForm form = new Telerik.WinControls.UI.RadForm();
                form.Text = MenuText;
                var formEntity = new MnGridView<EntityType>(dbContext);
                form.Controls.Add(formEntity);
                form.StartPosition = FormStartPosition.CenterParent;
                form.ShowDialog();
            };
            if (type == SubMenuType.Setting)
                SettingMenu.Items.Add(subMenuItem);
            if (type == SubMenuType.Security)
                SecurityMenu.Items.Add(subMenuItem);
        }
        public void AddSettingEntity<EntityType>(string MenuText = "") where EntityType : BaseEntity
        {
            AddSubMenuEntity<EntityType>(SubMenuType.Setting, DBContext, MenuText);
        }
        public void AddMainEntity<EntityType>(IBaseView<EntityType> winView = null) where EntityType : BaseEntity
        {

            MnPageViewPage<EntityType> tab;
            if (winView == null)
                tab = new MnPageViewPage<EntityType>(DBContext, this.Width);
            else
                tab = new MnPageViewPage<EntityType>(DBContext, this.Width, winView);
            MainPageView.Pages.Add(tab);
            ExistEntity.Add(typeof(EntityType));
        }

        #region Events
        void radMenuItemFilter_Click(object sender, EventArgs e)
        {

        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            RadPageViewPage indexTab = new RadPageViewPage();
            indexTab.Controls.Add(new IndexPanel(DBContext, ExistEntity, Width - 5));
            indexTab.Dock = DockStyle.Fill;
            indexTab.Title = indexTab.Text = "برگه اصلی";
            MainPageView.Pages.Insert(0, indexTab);
        }
        #endregion
    }
}
